package com.aliyun.messagemgt.application.service.Impl;


import com.aliyun.messagemgt.common.dto.send.MsgBody;
import com.aliyun.messagemgt.common.dto.send.MsgMiddleResult;
import com.aliyun.messagemgt.common.dto.send.MsgRecordVO;
import com.aliyun.messagemgt.application.service.MediaUrlHandler;
import com.aliyun.messagemgt.application.service.MsgMediaHandler;
import com.aliyun.messagemgt.application.service.WeixinMediaHandler;
import com.aliyun.messagemgt.common.enums.MediaLimitEnum;
import com.aliyun.messagemgt.common.enums.PushResultEnum;
import com.aliyun.messagemgt.util.MessageTypeUtil;
import com.aliyun.messagemgt.common.constant.PushConstant;
import com.aliyun.messagemgt.common.constants.push.MediaHandlerCodeConstant;
import com.aliyun.messagemgt.common.constants.push.WeChatHandlerCodeConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IDEA
 *
 * @author:zhangxingxing 0027012120
 * @date:2019/2/1
 * @time:17:08
 */
@Component
public class MsgMediaHandlerImpl implements MsgMediaHandler {

    @Autowired
    private MediaUrlHandler mediaUrlHandler;
    @Autowired
    private WeixinMediaHandler weixinMediaHandler;

    /**
     * 找出content 中的url 并处理，回填处理结果
     *
     * @param msgRecordVos
     */
    @Override
    public void findMediaAndTransfer(List<MsgRecordVO> msgRecordVos) {

        for (MsgRecordVO msgRecordVO : msgRecordVos) {
            List<MsgMiddleResult> results = msgRecordVO.getResults();
            List<MsgBody> msgBodies = msgRecordVO.getContents();
            // 取消息体判断, 类型无定义则返回类型错误 TEXT 类型 则不处理
            for (MsgBody msgBody : msgBodies) {
                String temp = msgBody.getType();
                String type = StringUtils.isEmpty(temp) ? temp : temp.toUpperCase();
                MediaLimitEnum typeEnum = MediaLimitEnum.getEnumByTypeName(type);
                // 如果类型不存在定义号的枚举类中 则回填相应的错误码并跳出循环
                if (typeEnum == null) {
                    // 生成具体的接收者维度的状态信息
                    MsgMiddleResult msgMiddleResult = new MsgMiddleResult()
                            .setReceivers(msgRecordVO.getReceivers())
                            .setPushResultCode(PushResultEnum.MEDIA_TYPE_ERROR.getErrorCode())
                            .setPushResultDescription(PushResultEnum.MEDIA_TYPE_ERROR.getDescription());
                    results.add(msgMiddleResult);
                    msgRecordVO.setReceivers(new ArrayList<>());
                    break;
                }
                //链接卡片消息类型时，不处理
                if (MediaLimitEnum.LINK_CARD.equals(typeEnum)) {
                    break;
                }
                // 如果类型不为text文本时 需要经过url处理
                if (!MediaLimitEnum.TEXT.equals(typeEnum)) {
                    // 注意：微信推送的图、音频和视频不存OSS服务器；图文和卡片才保存oss服务器，返回图片接口地址
                    int messageType = MessageTypeUtil.getIntMessageType(msgRecordVO.getMessageType());
                    if (messageType < PushConstant.WECHAT_NEWS) {
                        Integer res = mediaUrlHandler.urlHandle(msgBody);
                        // 当处理结果不为成功时，则回填相应的错误码并跳出循环
                        if (!MediaHandlerCodeConstant.SUCCESS.equals(res)) {
                            // 生成具体的接收者维度的状态信息
                            MsgMiddleResult msgMiddleResult = new MsgMiddleResult()
                                    .setReceivers(msgRecordVO.getReceivers())
                                    .setPushResultCode(PushResultEnum.getEnumByErrorCode(res).getErrorCode())
                                    .setPushResultDescription(PushResultEnum.getEnumByErrorCode(res).getDescription());
                            results.add(msgMiddleResult);
                            msgRecordVO.setReceivers(new ArrayList<>());
                            break;
                        }
                    }
                    // 注意：微信推送的图、音频和视频要上传微信做临时素材
                    if (messageType > PushConstant.WECHAT_NEWS) {
                        Integer uploadRes = null;
                        uploadRes = weixinMediaHandler.uploadTemporary(msgBody, msgRecordVO);
                        if (!WeChatHandlerCodeConstant.SUCCESS.equals(uploadRes)) {
                            // 生成具体的接收者维度的状态信息
                            MsgMiddleResult msgMiddleResult = new MsgMiddleResult()
                                    .setReceivers(msgRecordVO.getReceivers())
                                    .setPushResultCode(PushResultEnum.getEnumByErrorCode(uploadRes).getErrorCode())
                                    .setPushResultDescription(PushResultEnum.getEnumByErrorCode(uploadRes).getDescription());
                            results.add(msgMiddleResult);
                            msgRecordVO.setReceivers(new ArrayList<>());
                            break;
                        }
                    }
                }
            }
        }
    }

}
